package kr.saeildamil.androidstudty.ch19;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

public class CalcThread extends Thread {
	Handler mMainHandler;
	Handler mBackHandler;
	
	public CalcThread(Handler mMainHandler) {
		this.mMainHandler = mMainHandler;
	}
	
	@Override
	public void run() {
		super.run();
		Looper.prepare();
		mBackHandler = new Handler(){
			public void handleMessage(android.os.Message msg) {
				Message retmsg = new Message();
				switch(msg.what){
				case 0:
					try{Thread.sleep(200);
					}catch(InterruptedException e){}
					retmsg.what = 0;
					retmsg.arg1 = msg.arg1 * msg.arg1;
					break;
				case 1:
					try{Thread.sleep(200);
					}catch(InterruptedException e){}
					retmsg.what = 1;
					retmsg.obj = new Double(Math.sqrt((double)msg.arg1));
					break;
				}
				mMainHandler.sendMessage(retmsg);
				
			}
		};
		Looper.loop();
	}

}
